package pers.jing.android.helper.db

import app.cash.sqldelight.db.SqlDriver

class Database(driver: SqlDriver) {
    private val mDB: AppDatabase
    private val mDBQueries: AppDatabaseQueries

    init {
        //需要先执行创建语句
        AppDatabase.Schema.create(driver)
        mDB = AppDatabase(driver)
        mDBQueries = mDB.appDatabaseQueries
    }

    fun saveConfig(name: String, value: String) {
        val item = mDBQueries.selectConfigByName(name).executeAsOneOrNull()
        if (item == null) {
            mDBQueries.insertConfig(name, value)
        } else {
            mDBQueries.updateConfig(name, value)
        }
    }

    fun getConfig(name: String): String? {
        val item = mDBQueries.selectConfigByName(name).executeAsOneOrNull()
        return item?.cValue
    }

}